package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.InStoreMapper;
import com.it.mapper.ProductMapper;
import com.it.pojo.InStore;
import com.it.pojo.InStoreExample;
import com.it.pojo.Product;
import com.it.pojo.ProductExample;
import com.it.pojo.dto.InStoreDto;
import com.it.pojo.enums.InstoreStatus;
import com.it.pojo.enums.IsDel;
import com.it.pojo.enums.OutStoreStatus;
import com.it.service.InStoreService;
import com.it.util.CodeUtils;
import com.it.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: zgc
 * @version: 1.0
 * @description:
 * @date: 2023/12/26 14:02
 */
@Service
public class InStoreServiceImpl implements InStoreService {

    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private ProductMapper productMapper;


    public String getPrimaryKey() {
        InStoreExample example = new InStoreExample();
        List<InStore> list = inStoreMapper.selectByExample(example);
        return list.size() + 1 + "";
    }
    @Override
    public String instore(InStore inStore) {
        String msg;
        inStore.setInId(getPrimaryKey());
        inStore.setInCode(CodeUtils.getCodeByCountIT(Integer.parseInt(getPrimaryKey())));
        inStore.setInTime(new Date());
        inStore.setInStatus(OutStoreStatus.OUTSTORE.getStatusNum());
        inStore.setInDel(IsDel.NODEL.getStatusNum());
        int i = inStoreMapper.insertSelective(inStore);
        if (i>0){
            msg = "入库成功";
            productMapper.updateProductStatus(inStore.getProductId(),InstoreStatus.INSTORE.getStatusNum());
        }else {
            msg = "入库失败";
        }
        return msg;
    }

    //查看入库中未出库（InStatus=1）的信息
    @Override
    public List<InStore> selectByInStatus(String companyId) {
        List<InStore> instores = inStoreMapper.selectByInStatus(companyId);
        return instores;
    }

    @Override
    public InStore selectById(String inid) {
        InStore instore = inStoreMapper.selectByid(inid);
        return instore;
    }

    @Override
    public PageInfo<InStore> findPage(InStoreDto dto) {
        if (dto.getPageNo() == null) {
            dto.setPageNo(1);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        InStoreExample example = new InStoreExample();
        InStoreExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andInTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andInTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andInTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andInTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("in_time asc");
        List<InStore> list = inStoreMapper.selectByExample(example);
        PageInfo<InStore> pageInfo = new PageInfo<>(list, 5);
        return pageInfo;
    }

    @Override
    public List<InStore> findExcel(InStoreDto dto) {
        InStoreExample example = new InStoreExample();
        InStoreExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andInTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andInTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andInTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andInTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("in_time asc");
        return inStoreMapper.selectByExample(example);
    }

}
